﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Oracle.DataAccess.Client;

namespace TP2
{
    public partial class AddPhysicalClientForm : Form
    {
        
        
        PhysicalClient physicalClientFr;
        
        public AddPhysicalClientForm(PhysicalClient PClient)
        {
            physicalClientFr = PClient;
            InitializeComponent();
        }

        private void SelectFromCredit(string client_type)
        {
            DBWorkClass dbWork = new DBWorkClass();
            ClassRateType selectMas = new ClassRateType();
            selectMas = dbWork.SelectFromCreditRate(client_type);
            int j = 0;
            if (selectMas.id_rate.Count == 0)
            {
                dataGridView1.RowCount = 1;
                dataGridView1[0, 0].Value = "";
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";
            }
            else { dataGridView1.RowCount = selectMas.id_rate.Count; }
            while (j < selectMas.id_rate.Count)
            {
                dataGridView1[0, j].Value = (selectMas.title[j].ToString());
                dataGridView1[1, j].Value = selectMas.procent[j].ToString();
                dataGridView1[2, j].Value = selectMas.period[j].ToString();
                j++;
            }
        }

        private void SelectFromAccumulation(string client_type)
        {
            DBWorkClass dbWork = new DBWorkClass();
            ClassRateType selectMas = new ClassRateType();
            selectMas = dbWork.SelectFromAccumulationRate(client_type);
            
            if (selectMas.id_rate.Count == 0)
            {
                dataGridView1.RowCount = 1;
                dataGridView1[0, 0].Value = "";
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";
            }
            else { dataGridView1.RowCount = selectMas.id_rate.Count; }
            int j = 0;
            while (j < selectMas.id_rate.Count)
            {
                dataGridView1[0, j].Value = (selectMas.title[j].ToString());
                dataGridView1[1, j].Value = selectMas.procent[j].ToString();
                dataGridView1[2, j].Value = selectMas.period[j].ToString();
                j++;
            }
        }

        private void CleanArrayList()
        {
            
        }

        public void Refresh()
        {
            this.CleanArrayList();
            string client_type = "физический";
            if (comboBox1.SelectedIndex == 0)
            {
                this.SelectFromCredit(client_type);
            }
            else
            {
                this.SelectFromAccumulation(client_type); 
            }                                   
        }
        
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Refresh();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                string pasport = maskedTextBoxPaport.Text;
                string fio = textBoxFio.Text;
                int index = dataGridView1.CurrentRow.Index;
                int period = Convert.ToInt32(dataGridView1[2, index].Value);
                DateTime start_date = dateTimePicker2.Value;
                DateTime berth_date = dateTimePicker1.Value;
                Double procent = Convert.ToDouble(dataGridView1[1, index].Value);
                int deposit = Convert.ToInt32(textBoxDeposit.Text);
                DBWorkClass dbWork = new DBWorkClass();
                int deposits = Convert.ToInt32(textBoxDeposit.Text);
                dbWork.InsertPhysicalClient(pasport, fio, period, start_date, berth_date, comboBox1.SelectedIndex, deposit, procent, deposits);
                physicalClientFr.refreshTable();
                this.Close();
            }
            catch (FormatException) { MessageBox.Show("ошибка во входных данных"); }
        }

        private void buttonCalc_Click(object sender, EventArgs e)
        {
            try
            {            
                    LogicClass logicObj = new LogicClass();
                    int index = dataGridView1.CurrentRow.Index;
                    int summa = Convert.ToInt32(textBoxDeposit.Text);

                    if (comboBox1.SelectedIndex == 0)
                    {
                        Double procent = Convert.ToDouble(dataGridView1[1, index].Value);
                        labelRezult.Text = "сумма долга =";
                        labelSumma.Text = logicObj.GetCreditSumm(summa, procent).ToString();
                    }
                    if (comboBox1.SelectedIndex == 1)
                    {
                        int period = Convert.ToInt32(dataGridView1[2, index].Value);
                        Double procent = Convert.ToDouble(dataGridView1[1, index].Value);
                        labelRezult.Text = "сумма на счёте=";
                        labelSumma.Text = logicObj.UpdateDeposit(summa, period, procent).ToString();
                    }
                
            }
            catch (FormatException) { MessageBox.Show("чё вы там во входных данных накосячили!"); }
        }

        
    }
}
